package sdk.pay.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import java.util.List;

public final class PayPackageUtil {
    /**
     * 判断应用是否可用
     *
     * @param context,"com.tencent.mm","com.eg.android.AlipayGphone"
     * @return
     */
    private static boolean isAvailable(Context context, String packageName) {
        PackageManager packageManager = context.getPackageManager();// 获取packagemanager
        List<PackageInfo> infos = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (infos != null) {
            for (PackageInfo info : infos) {
                if (info.packageName.equals(packageName)) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean isWeChatAvailable(Context context) {
        return isAvailable(context, "com.tencent.mm");
    }

    public static boolean isAlipayAvailable(Context context) {
        return isAvailable(context, "com.eg.android.AlipayGphone");
    }

    public static boolean isQQpayAvailable(Context context) {
        return isAvailable(context, "com.tencent.mobileqq") || isAvailable(context, "com.tencent.tim");
    }
}
